 package com.oanda.blackberry.util;
 
 import com.oanda.blackberry.restapi.Account;
 import com.oanda.blackberry.restapi.AccountEntry;
 import com.oanda.blackberry.restapi.AccountStatus;
 import com.oanda.blackberry.restapi.OrderListResponse;
 import com.oanda.blackberry.restapi.OrderPollResponse;
 import com.oanda.blackberry.restapi.PositionsListResponse;
 import com.oanda.blackberry.restapi.Quote;
 import com.oanda.blackberry.restapi.Rate;
 import com.oanda.blackberry.restapi.Symbol;
 import com.oanda.blackberry.restapi.TradeListResponse;
 import com.oanda.blackberry.restapi.TradePollResponse;
 import com.oanda.blackberry.restapi.User;
 import com.oanda.blackberry.util.log.Logger;
 
 public class Model extends AbstractModel
 {
   private static Model staticModel = null;
   public static final boolean DEBUG = true;
   public static final boolean SIMULATEINTERNET = true;
   public static final boolean DEVELOPMENT = true;
   public static final String VERSION = "1.2.4";
   public static final String API_KEY = "0325ee6232373738";
   boolean isStatusShown = false;
 
   boolean isMainMenuShown = false;
   User user;
   Account account;
   String statusMessage;
   Settings settings;
   Rate rate;
   static final int historyPointsCandlestick = 47;
   static final int historyPointsRibbon = 183;
 
   private Model()
   {
     staticModel = this;
   }
 
   public static synchronized Model getModel() {
     if (staticModel == null) {
       new Model();
     }
     return staticModel;
   }
 
   public User getUser() {
     return getModel().user;
   }
 
   public void setUser(User user) {
     this.propertyChangeSupport.firePropertyChange("setUser", getModel().user, user);
     getModel().user = user;
   }
 
   public void setRateSybmol(Rate rate) {
     if (rate == null) return;
 
     rate.sortSymbols();
 
     this.propertyChangeSupport.firePropertyChange("setRateSymbol", getRateSymbol(), rate);
     if (getRate() != null)
       getRate().setSymbols(rate.getSymbols());
   }
 
   public void setAccount(Account account) {
     if (account == null) return;
 
     this.propertyChangeSupport.firePropertyChange("setAccount", getModel().getSettings().account, account);
     getModel().getSettings().setAccount(account);
   }
 
   public void setAccountStatus(AccountStatus s) {
     this.propertyChangeSupport.firePropertyChange("setAccountStatus", s, s);
   }
 
   public Account getAccount() {
     return getModel().getSettings().getAccount();
   }
 
   public Rate getRate() {
     return getModel().getSettings().getRate();
   }
 
   public Symbol[] getRateSymbol() {
     return getModel().getSettings().getRate().getSymbols();
   }
 
   public String getRateSymbolsSelected()
   {
     StringBuffer buffer = new StringBuffer();
     for (int i = 0; (getRateSymbol() != null) && (i < getRateSymbol().length); i++) {
       Symbol object = getRateSymbol()[i];
       if ((object != null) && (object.isActive())) {
         buffer.append(object.getSymbol());
         buffer.append(',');
       }
 
     }
 
     if (buffer.length() > 1) {
       buffer = buffer.deleteCharAt(buffer.length() - 1);
     }
     return buffer.toString();
   }
 
   public String getRateSymbolsAll() {
     StringBuffer buffer = new StringBuffer();
     for (int i = 0; (getRateSymbol() != null) && (i < getRateSymbol().length); i++) {
       Symbol object = getRateSymbol()[i];
       if (object != null) {
         buffer.append(object.getSymbol());
         buffer.append(',');
       }
 
     }
 
     if (buffer.length() > 1) {
       buffer = buffer.deleteCharAt(buffer.length() - 1);
     }
     return buffer.toString();
   }
 
   public synchronized String getSessionToken() {
     return getModel().getSettings().getSessionToken();
   }
 
   public synchronized void setSessionToken(String sessionToken) {
     getModel().getSettings().setSessionToken(sessionToken);
 
     this.propertyChangeSupport.firePropertyChange("sessionToken", getModel().getSettings().getSessionToken(), sessionToken);
   }
 
   public synchronized String getStatusMessage()
   {
     return getModel().statusMessage;
   }
 
   public synchronized void setStatusMessage(String message) {
     this.propertyChangeSupport.firePropertyChange("setStatusMessage", getModel().statusMessage, message);
     getModel().statusMessage = message;
   }
 
   public boolean getIsStatusShown() {
     return getModel().isStatusShown;
   }
 
   public void setIsStatusShown(boolean isStatusShown) {
     this.propertyChangeSupport.firePropertyChange("isStatusShown", new Boolean(getModel().isStatusShown), new Boolean(isStatusShown));
 
     getModel().isStatusShown = isStatusShown;
   }
 
   public boolean getIsMainMenuShown() {
     return getModel().isStatusShown;
   }
 
   public void setIsMainMenuShown(boolean isMainMenuShown) {
     this.propertyChangeSupport.firePropertyChange("isMainMenuShown", new Boolean(getModel().isMainMenuShown), new Boolean(isMainMenuShown));
 
     getModel().isMainMenuShown = isMainMenuShown;
   }
 
   public void setActiveAccountIndex(int index)
   {
     this.propertyChangeSupport.firePropertyChange("setActiveAccountIndex", new Integer(getActiveAccountIndex()), new Integer(index));
 
     getModel().getSettings().setAccountEntriesIndex(index);
   }
 
   public int getActiveAccountIndex() {
     return getModel().getSettings().getAccountEntriesIndex();
   }
 
   public void setActiveSymbol(String symbol)
   {
     this.propertyChangeSupport.firePropertyChange("setActiveSymbol", "", symbol);
 
     if (getRate() == null) return;
     if (getRate().getSymbols() == null) return;
 
     for (int i = 0; i < getRate().getSymbols().length; i++)
     {
       if (getRate().getSymbols()[i].getSymbol().equals(symbol)) {
         getRate().getSymbols()[i].setActive(true);
         return;
       }
     }
   }
 
   public void setInactiveSymbol(String symbol)
   {
     this.propertyChangeSupport.firePropertyChange("notUseSymbol", "", symbol);
 
     if (getRate() == null) return;
     if (getRate().getSymbols() == null) return;
 
     for (int i = 0; i < getRate().getSymbols().length; i++)
     {
       if (getRate().getSymbols()[i].getSymbol().equals(symbol)) {
         getRate().getSymbols()[i].setActive(false);
         return;
       }
     }
   }
 
   public void setActiveAccount(String accountId) {
     this.propertyChangeSupport.firePropertyChange("setActiveAccount", getActiveAccountId(), accountId);
 
     if (getAccount() == null) return;
     if (getAccount().getAccountEntrys() == null) return;
 
     for (int i = 0; i < getAccount().getAccountEntrys().length; i++)
     {
       if (getAccount().getAccountEntrys()[i].getId().equals(accountId)) {
         getAccount().getAccountEntrys()[i].setActive(true);
         setActiveAccountIndex(i);
       } else {
         getAccount().getAccountEntrys()[i].setActive(false);
       }
     }
   }
 
   public boolean getActiveSymbol(String symbol) {
     if (getRate() == null) return false;
     if ((getRate() != null) && (getRate().getSymbols() == null)) return false;
 
     for (int i = 0; i < getRate().getSymbols().length; i++) {
       if (getRate().getSymbols()[i].getSymbol().equals(symbol)) {
         return getRate().getSymbols()[i].isActive();
       }
     }
     Logger.log("getActiveSymbol:  unable to find " + symbol + " return null");
     return false;
   }
 
   public String getActiveAccountId() {
     if (getAccount() == null) return null;
     if ((getAccount() != null) && (getAccount().getAccountEntrys() == null)) return null;
 
     for (int i = 0; i < getAccount().getAccountEntrys().length; i++) {
       if (getAccount().getAccountEntrys()[i].getActive()) {
         return getAccount().getAccountEntrys()[i].getId();
       }
     }
     Logger.log("getActiveAccountId:  unable to find ActiveAccount return null");
     return null;
   }
 
   public AccountEntry getActiveAccountEntry() {
     Logger.log("Model getActiveAccountEntry");
     for (int i = 0; (getAccount() != null) && (getAccount().getAccountEntrys() != null) && (i < getAccount().getAccountEntrys().length); i++) {
       if (getAccount().getAccountEntrys()[i].getActive()) {
         return getAccount().getAccountEntrys()[i];
       }
     }
     Logger.log("getActiveAccountEntry:  return null");
     return null;
   }
 
   public String getAccountNameById(String id) {
     if (getAccount() == null) return null;
     if ((getAccount() != null) && (getAccount().getAccountEntrys() == null)) return null;
 
     for (int i = 0; (id != null) && (i < getAccount().getAccountEntrys().length); i++) {
       if (getAccount().getAccountEntrys()[i].getId().equalsIgnoreCase(id)) {
         return getAccount().getAccountEntrys()[i].getName();
       }
     }
     Logger.log("getAccountNameById:  unable to find Account ID match return null");
     return null;
   }
 
   public String getAccountCurrencyById(String id) {
     if (getAccount() == null) return null;
     if ((getAccount() != null) && (getAccount().getAccountEntrys() == null)) return null;
 
     for (int i = 0; (id != null) && (i < getAccount().getAccountEntrys().length); i++) {
       if (getAccount().getAccountEntrys()[i].getId().equalsIgnoreCase(id)) {
         return getAccount().getAccountEntrys()[i].getHomecurr();
       }
     }
     Logger.log("getAccountCurrencyById:  unable to find Account ID match return null");
     return null;
   }
 
   public void setSelectedPlatformIndex(int index) {
     this.propertyChangeSupport.firePropertyChange("setUrlIndex", new Integer(getUrlIndex()), new Integer(index));
 
     getModel().getSettings().setUrlIndex(index);
   }
 
   public int getUrlIndex()
   {
     return getModel().getSettings().getUrlIndex();
   }
 
   public void setSettings(Settings settings) {
     this.propertyChangeSupport.firePropertyChange("setSettings", this.settings, settings);
     this.settings = settings;
   }
 
   public Settings getSettings() {
     return this.settings;
   }
 
   public void setUserSettings(UserSettings user) {
     this.propertyChangeSupport.firePropertyChange("setUserSettings", getModel().getSettings().getUserAccountSettings(user.getIndex()), user);
 
     getModel().getSettings().setUserAccountSettings(user, getModel().getUrlIndex());
   }
 
   public UserSettings getUserSettings(int index) {
     return getModel().getSettings().getUserAccountSettings(index);
   }
 
   public Symbol[] getListSymbols()
   {
     if (getModel().getSettings().getRate() == null) {
       return null;
     }
     return getModel().getSettings().getRate().getSymbols();
   }
 
   public void setRateQuotes(Rate rate) {
     Logger.log("setRatesQuotes");
     this.propertyChangeSupport.firePropertyChange("setRateQuote", getRateQuotes(), rate);
 
     if (getRate() != null) {
       getRate(); Rate.setSince(Rate.getSince());
       getRate().setQuotes(rate.getQuotes());
     }
   }
 
   public Quote[] getRateQuotes() {
     Logger.log("getRateQuotes");
     if (getModel().getSettings().getRate() == null) {
       return null;
     }
     return getModel().getSettings().getRate().getQuotes();
   }
 
   public void setRateQuotesAll(Rate rate) {
     this.propertyChangeSupport.firePropertyChange("setRateQuoteAll", getRateQuotesAll(), rate);
     if (getRate() != null) {
       getRate().setSinceAll(rate.getSinceAll());
       getRate().setQuotesAll(rate.getQuotesAll());
     }
   }
 
   public Quote[] getRateQuotesAll() {
     Logger.log("getRateQuotesAll");
     if (getModel().getSettings().getRate() == null) {
       return null;
     }
     return getModel().getSettings().getRate().getQuotesAll();
   }
 
   public int getConnectionSuffixIndex() {
     return getModel().getSettings().getConnectionSuffixIndex();
   }
 
   public void setConnectionSuffixIndex(int index) {
     this.propertyChangeSupport.firePropertyChange("setConnectionSuffixIndex", new Integer(getConnectionSuffixIndex()), new Integer(index));
 
     getModel().getSettings().setConnectionSuffixIndex(index);
   }
 
   public void setTrustAll(boolean checked) {
     this.propertyChangeSupport.firePropertyChange("setTrustAll", new Boolean(isTrustAll()), new Boolean(checked));
 
     getModel().getSettings().setTrustAll(checked);
   }
 
   public boolean isTrustAll() {
     return getModel().getSettings().isTrustAll();
   }
 
   public void setEnableHttpsRedirect(boolean checked) {
     this.propertyChangeSupport.firePropertyChange("enableHttpsRedirect", new Boolean(isEnableHttpsRedirect()), new Boolean(checked));
 
     getModel().getSettings().setEnableHttpsRedirec(checked);
   }
 
   public boolean isEnableHttpsRedirect() {
     return getModel().getSettings().isEnableHttpsRedirec();
   }
 
   public void setEndToEndRequired(boolean checked) {
     this.propertyChangeSupport.firePropertyChange("endToEndRequired", new Boolean(isEndToEndRequired()), new Boolean(checked));
 
     getModel().getSettings().setEndToEndRequired(checked);
   }
 
   public boolean isEndToEndRequired() {
     return getModel().getSettings().isEndToEndRequired();
   }
 
   public Symbol getSymbol(String symbolName) {
     return getRate().getSymbol(symbolName);
   }
 
   public void setNewTradeOpenSell(Quote quote) {
     this.propertyChangeSupport.firePropertyChange("newTradeOpenSell", null, quote);
   }
 
   public void setNewTradeOpenBuy(Quote quote)
   {
     this.propertyChangeSupport.firePropertyChange("newTradeOpenBuy", null, quote);
   }
 
   public void setRateHistory(Rate rates)
   {
     this.propertyChangeSupport.firePropertyChange("setRateHistory", null, rates);
 
     getRate().setPricePoints(rates.getPricePoints());
   }
 
   public void setRateHistoryUpdate(Rate rates) {
     this.propertyChangeSupport.firePropertyChange("setRateHistoryUpdate", null, rates);
   }
 
   void setTradeList(TradeListResponse[] tradeListResponse)
   {
     if (tradeListResponse == null) {
       Logger.log("!!! tradeListResponse == null");
     }
     this.propertyChangeSupport.firePropertyChange("setTradeList", null, tradeListResponse);
   }
 
   void setPositionList(PositionsListResponse[] positionsListResponse)
   {
     this.propertyChangeSupport.firePropertyChange("setPositionsList", null, positionsListResponse);
   }
 
   void setOrderList(OrderListResponse[] orderListResponse)
   {
     this.propertyChangeSupport.firePropertyChange("setOrderList", null, orderListResponse);
   }
 
   void setTradePollResponse(TradePollResponse tradePollResponse)
   {
     this.propertyChangeSupport.firePropertyChange("setTradePollResponse", null, tradePollResponse);
   }
 
   void setOrderPollResponse(OrderPollResponse tradeOrderResponse)
   {
     this.propertyChangeSupport.firePropertyChange("setOrderPollResponse", null, tradeOrderResponse);
   }
 
   public int getHistoryChartTypeIndex()
   {
     return getModel().getSettings().getHistoryChartTypeIndex();
   }
 
   public String getHistoryTimeInterval() {
     if (getModel() == null) return null;
     if (getModel().getSettings() == null) return null;
 
     return getModel().getSettings().getHistoryTimeInterval();
   }
 
   public void setHistoryChartTypeIndex(int historyChartTypeIndex) {
     getModel().getSettings().setHistoryChartTypeIndex(historyChartTypeIndex);
   }
 
   public void setHistoryTimeInterval(String historyTimeInterval) {
     getModel().getSettings().setHistoryTimeInterval(historyTimeInterval);
   }
 
   public static String getVERSION() {
     return "1.2.4";
   }
 
   public boolean isShowInternetTrafficWarning() {
     return getModel().getSettings().isShowInternetTrafficWarning();
   }
 
   public void setShowInternetTrafficWarning(boolean b) {
     getModel().getSettings().setShowInternetTrafficWarning(this.isMainMenuShown);
   }
 
   public static int getHistoryPoints(int historyChartType) {
     switch (historyChartType) {
     case 0:
       return 183;
     case 1:
       return 47;
     }
 
     return 47;
   }
}

